package com.lifo.shoppingcart.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.lifo.good.bean.model.HfGoods;
import com.lifo.good.service.SearchGoodsService;
import com.lifo.shoppingcart.bean.HfShoppingCart;
import com.lifo.shoppingcart.bean.vo.HfShoppingCartVo;
import com.lifo.shoppingcart.service.ShoppingCartManagerService;
import com.lifo.taobaoshop.bean.model.HfTaobaoShop;
import com.lifo.taobaoshop.service.HfTaobaoShopManagerService;
import com.lifo.utils.wrapper.WrapperJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by Administrator on 2018/6/25 0025.
 */
@Slf4j
@Controller
public class SearchShoppingCartController {

    @Autowired
    private ShoppingCartManagerService shoppingCartManagerService;

    @Autowired
    private HfTaobaoShopManagerService hfTaobaoShopManagerService;


    @Autowired
    private SearchGoodsService searchGoodsService;


    @RequestMapping(value = "/cart/goCart")
    public String goCart(HttpServletRequest request, Model model) throws JsonProcessingException {
        int userId = Integer.parseInt(request.getSession().getAttribute("userId").toString());
        List<HfShoppingCart> list = shoppingCartManagerService.searchHfShoppingCartsByUserIdGroupByConditions(userId);
        List<HfShoppingCartVo> voList = this.mergeGoods(list);
        model.addAttribute("shoppingCartList", voList);
        log.debug("购物车即将展示的商品==>{}",new WrapperJson().toJson(voList));

        //店铺信息 begin
        HashSet hs = new HashSet(list);
        list.clear();
        list.addAll(hs);
        List<HfTaobaoShop> hfTaobaoShopList = new ArrayList<>();
        for (HfShoppingCart hf : list) {
            HfTaobaoShop hfTaobaoShop = hfTaobaoShopManagerService.getHfTaobaoShopByShopId(hf.getShopId());
            hfTaobaoShopList.add(hfTaobaoShop);
        }
        model.addAttribute("HfTaobaoShopList", hfTaobaoShopList);
        //店铺信息 end
        return "/center/shoppingcart";
    }

    /**
     * 合并购物车中属性相同的商品
     * @param list
     * @return
     */
    public List<HfShoppingCartVo> mergeGoods(List<HfShoppingCart> list) throws JsonProcessingException {
        List<HfShoppingCartVo> listVo = new ArrayList<>();
        for(HfShoppingCart hfShoppingCart:list){
            HfGoods hfGoods = searchGoodsService.searchHfGoodsByHfUid(Integer.parseInt(String.valueOf(hfShoppingCart.getHfGoodId())));
            HfShoppingCartVo vo = new HfShoppingCartVo();
            BeanUtils.copyProperties(hfShoppingCart,vo);
            vo.setSmallPicture(hfGoods.getSmallPicture().split(";")[0]);
            listVo.add(vo);
        }
        return listVo;
    }

    /**
     * 个人中心购物车-立即下单
     * @return
     */
    @RequestMapping(value = "/cart/showGoods")
    public @ResponseBody String showShoppingCartGoods(){

       /* int goodCount =  shoppingCartManagerService.countByUserId(hfShoppingCart.getUserId());
        request.getSession().setAttribute("shoppingCartGoodCount",goodCount);*/

        return null;
    }
}
